﻿using System;
using System.Web;
using System.Data;
using System.Collections.Generic;

using VIT.Web.UI;
using VIT.Provider;
using VIT.Web.Controls;
using VIT.Library.Web;
using VIT.DataHelper.DataAccess;

namespace VIT.Web.Admin
{
    public partial class CompanyConfig : VITComponent
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var company = VITData.GetObject<CompanyDetail>(o => o.Id == 1);
                loadData(company);
            }
        }

        private void loadData(CompanyDetail company)
        {
            txtAddress.Text = company.Address;
            txtCompanyName.Text = company.CompanyName;
            txtMail.Text = company.Email;
            txtMailHost.Text = company.MailHost;
            txtMailServer.Text = company.MailServer;
            txtPass.Text = company.Pass;
            txtPhone.Text = company.Phone;
            txtFax.Text = company.Fax;
        }

        private void save()
        {
            string user = HttpContext.Current.Session["userName"].ToString();

            CompanyDetail company = new CompanyDetail();
            company.Id = 1;
            company.Address = txtAddress.Text;
            company.CompanyName = txtCompanyName.Text;
            company.Email = txtMail.Text;
            company.MailHost = txtMailHost.Text;
            company.MailServer = txtMailServer.Text;
            company.Pass = txtPass.Text;
            company.Phone = txtPhone.Text;
            company.Fax = txtFax.Text;
            company.ModifyByUser = user;
            company.ModifyDate = DateTime.Now;

            if (VITData.UpdateObject<CompanyDetail>(x => x.Id == company.Id, company))
                setTrueVisible(lblThanhCong);
            else setTrueVisible(lblThatBai);
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;

            control.Visible = true;
        }

        protected void Apply_Click(object sender, EventArgs e)
        {
            save();
        }
        protected void Save_Click(object sender, EventArgs e)
        {
            save();
            RedirectComponent("Home");
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            RedirectComponent("Home");
        }
    }
}
